package com.ys.service.sys.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.dto.DtreeSqlDto;
import com.ys.entity.sys.YsMenu;
import com.ys.entity.sys.YsRowClickGrid;
import com.ys.enume.sys.YsMenuEnum;
import com.ys.mapper.sys.YsMenuMapper;
import com.ys.mapper.sys.YsRowClickGridMapper;
import com.ys.service.sys.YsRowClickGridService;
import com.ys.vo.R;

@Service("ysRowClickGridService")
public class YsRowClickGridServiceImpl implements YsRowClickGridService {
	
	@Autowired
	private YsMenuMapper ysMenuMapper;
	@Autowired
	private YsRowClickGridMapper ysRowClickGridMapper;

	@Override
	public DtreeSqlDto dtree() {
		
		DtreeSqlDto.StatusDto status = DtreeSqlDto.StatusDto.init(200, "");

		List<DtreeSqlDto.DataDto> data = new ArrayList<DtreeSqlDto.DataDto>();
		List<YsRowClickGrid> listEntity = new YsRowClickGrid()
				.queryList();
		
		// 一级菜单
		for(YsRowClickGrid entity : listEntity) {
			String id1 = entity.getId().toString();
			String title1 = entity.getName();
			String parentId1 = null;
			
			// 二级菜单
			List<DtreeSqlDto.DataDto> children = new ArrayList<DtreeSqlDto.DataDto>();
			
			DtreeSqlDto.DataDto dataDto1 = new DtreeSqlDto.DataDto(id1, title1, parentId1, DtreeSqlDto.DataDto.initCheckArr(null), children);
			data.add(dataDto1);
		}
		return new DtreeSqlDto(status, data);
	}

	@Override
	@Transactional
	public R deleteTrue(Long id) {
		if (id == null) {
			return R.error("ID不能为空");
		}
		
//		// 删除角色-按钮
//		sysMapper.deleteTrueSysRoleButtonByTsDictionaryForModuleId(id);
//		
//		// 删除按钮
//		tsDictionaryForModuleMapper.deleteTrue(id);
		
		return R.ok();
	}
	
	@Override
	public R add(Map<String, Object> params) {
		
		YsRowClickGrid entity = JSON.parseObject(JSON.toJSONString(params), YsRowClickGrid.class);
		
		ysRowClickGridMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}
	
	@Override
	public R edit(Map<String, Object> params) {

		YsMenu entity = JSON.parseObject(JSON.toJSONString(params), YsMenu.class);
		
		String templetType = entity.getTempletType();
		if (templetType.equals(YsMenuEnum.TempletType.LIST.value) || templetType.equals(YsMenuEnum.TempletType.TREE_WITH_LIST.value)) {
			entity.setTempletPath("sys/common/templet_list");
		}
		
		ysMenuMapper.update(entity);
		return R.ok("操作成功");
	}
}
